import { ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';
import { CommonModule } from '@angular/common';
import {
  NbPasswordAuthStrategy,
  NbAuthModule,
  NbAuthJWTToken,
} from '@nebular/auth';
import { of as observableOf } from 'rxjs';

import { httpInterceptorProviders } from '../http-interceptors';
import { AuthGuard } from './utils/auth-guard.service';
import { DebounceService } from './utils/debounce';
import { LayoutService } from './utils/layout.service';

import { UserData } from './data/users';
import { ProfitBarAnimationChartData } from './data/profit-bar-animation-chart';
import { StatsBarData } from './data/stats-bar';
import { EarningData } from './data/earning';
import { OrdersChartData } from './data/orders-chart';
import { ProfitChartData } from './data/profit-chart';
import { OrdersProfitChartData } from './data/orders-profit-chart';
import { ProgressSectionBarData } from './data/progress-section';

import { UserService } from './service/users.service';
import { ProfitBarAnimationChartService } from './service/profit-bar-animation-chart.service';
import { StatsBarService } from './service/stats-bar.service';
import { EarningService } from './service/earning.service';
import { OrdersChartService } from './service/orders-chart.service';
import { ProfitChartService } from './service/profit-chart.service';
import { OrdersProfitChartService } from './service/orders-profit-chart.service';
import { ProgressBarService } from './service/progress-section.service';

const DATA_SERVICES = [
  { provide: UserData, useClass: UserService },
  { provide: ProfitBarAnimationChartData, useClass: ProfitBarAnimationChartService },
  { provide: StatsBarData, useClass: StatsBarService },
  { provide: EarningData, useClass: EarningService },
  { provide: OrdersChartData, useClass: OrdersChartService },
  { provide: ProfitChartData, useClass: ProfitChartService },
  { provide: OrdersProfitChartData, useClass: OrdersProfitChartService },
  { provide: ProgressSectionBarData, useClass: ProgressBarService },
]

export const NB_CORE_PROVIDERS = [
  DebounceService,
  AuthGuard,
  LayoutService,
  httpInterceptorProviders,
  ...DATA_SERVICES,
  ...NbAuthModule.forRoot({
    strategies: [
      NbPasswordAuthStrategy.setup({
        name: 'password',
        baseEndpoint: 'https://api.sheencity.com',
        login: {
          endpoint: '/token',
          method: 'post',
          redirect: {
            success: '/pages/dashboard',
            failure: '/auth'
          }
        },
        token: {
          class: NbAuthJWTToken,
          key: 'value'
        }
      })
    ],
    forms: {}
  }).providers,
]

@NgModule({
  imports: [
    CommonModule,
  ],
  exports: [
    NbAuthModule,
  ],
  declarations: [],
})
export class CoreModule {
  constructor(@Optional() @SkipSelf() parentModule: CoreModule) {

  }

  static forRoot(): ModuleWithProviders<CoreModule> {
    return {
      ngModule: CoreModule,
      providers: [
        ...NB_CORE_PROVIDERS,
      ],
    };
  }
}
